import { queryCompanies } from '@/pages/SystemUser/service';
import { verifyId } from '@/utils/checkIdNumber';
import { useModel } from 'umi';
import { Role } from '@/utils/types.d';
import { phoneNumberReg, specialCharReg, zhReg } from '@/utils/utils';
import { PlusOutlined } from '@ant-design/icons';
import { ModalForm, ProFormSelect, ProFormText } from '@ant-design/pro-components';
import { Button, message } from 'antd';
import { createToller } from '../service';
import type { CreateTollCollector } from '../types';
// import { queryParkinglotNames } from '@/pages/Parking/Spot/service';

export default (props: { reload: () => void }) => {
  const { initialState } = useModel('@@initialState');
  const userRole = initialState?.currentUser?.role;
  return (
    <ModalForm<CreateTollCollector>
      title="创建收费人员"
      layout="horizontal"
      width="40vw"
      modalProps={{ destroyOnClose: true }}
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      trigger={
        <Button type="primary">
          <PlusOutlined /> 创建
        </Button>
      }
      onFinish={async (values) => {
        const res = await createToller(values);
        if (res.success) {
          message.success({
            content: res.message,
            key: 'add_toller',
          });
          props.reload();
          return true;
        }
        message.error({
          content: res.message,
          key: 'add_toller',
        });
        return false;
      }}
    >
      <ProFormText
        required
        label="员工账号"
        placeholder="设置账号"
        name="account"
        rules={[
          {
            validator: (_, val) => {
              if (zhReg.test(val) || specialCharReg.test(val))
                return Promise.reject(new Error('员工账号不能含有汉字或特殊字符'));
              if (!val || val.length < 4)
                return Promise.reject(new Error('员工账号长度不得低于4位'));
              return Promise.resolve(val);
            },
          },
        ]}
        initialValue=""
      />
      <ProFormText
        required
        label="员工姓名"
        placeholder="请填写真实姓名"
        name="name"
        rules={[
          {
            validator: (_, val) => {
              if (!val || val.length < 2)
                return Promise.reject(new Error('用户名称长度不得低于2位'));
              if (!zhReg.test(val) || specialCharReg.test(val))
                return Promise.reject(new Error('请填写真实的中文姓名'));
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProFormText.Password
        required
        label="登录密码"
        placeholder="设置登录密码"
        name="password"
        rules={[
          {
            validator: (_, val) => {
              if (zhReg.test(val)) return Promise.reject(new Error('登录密码不符合要求'));
              if (!val || val.length < 6)
                return Promise.reject(new Error('用户密码长度不得低于6位'));
              if (val === '123456') return Promise.reject(new Error('密码强度过低'));
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProFormText
        name="identificationNumber"
        label="身份证号"
        placeholder="请输入身份证号"
        rules={[
          {
            validator: (_, val) => {
              if (val) {
                const res = verifyId(String(val));
                if (!res.enable) return Promise.reject(new Error(res.message));
              }
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProFormText
        name="phoneNumber"
        label="手机号码"
        placeholder="请输入手机号"
        rules={[
          {
            validator: (_, val) => {
              if (val && !phoneNumberReg.test(val))
                return Promise.reject(new Error('手机号不符合规范'));
              return Promise.resolve(val);
            },
          },
        ]}
      />
      {/* <ProFormSelect
        name="parkingLots"
        label="选择路段"
        showSearch
        mode="multiple"
        debounceTime={300}
        placeholder="请选择路段"
        rules={[
          {
            required: true,
            message: '请选择路段',
            validator: (_, val) => {
              if (!val || val.length <= 20) {
                return Promise.resolve(val);
              }
              return Promise.reject(new Error('执勤路段最多选择20个'));
            },
          },
        ]}
        request={(val) => queryParkinglotNames(val.keyWords)}
      /> */}
      {userRole === Role.ADMIN && (
        <ProFormSelect
          name="ownerId"
          label="用户归属公司"
          debounceTime={300}
          showSearch
          rules={[{ required: true, message: '请选择用户目标公司' }]}
          placeholder="请选择归属公司(如未找到目标公司，请优先创建[企业管理者])"
          request={(val) => queryCompanies(val.keyWords, false)}
        />
      )}
    </ModalForm>
  );
};
